/*
 * SqlComm.java
 *
 * Created on December 11, 2007, 5:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sqlComm;

import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;

/**
 * 
 * @author Daniel Peka?, xpekar04 a David Gargul?k, xgargu01
 */
public final class SqlComm {
    static SqlComm myInstance = null;
    Connection conn = null;
    /** Creates a new instance of SqlComm */
    public SqlComm() {
        if(myInstance != null) return;
        myInstance = this;
        // register driver
        
    }
    
    /**
     * Initialize the connection to the DB
     * @param url URL
     * @param user Username
     * @param password Password
     */
    public void init(String url, String user, String password) throws Exception {
        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//            String url = "jdbc:oracle:thin:login/heslo@adela.dcse.fee.vutbr.cz:1521:stud" ;
            conn = DriverManager.getConnection( url, user, password );
            
        }
        catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * Executes the query given in the parameter
     * @param query Query to execute
     */
    public ResultSet doQuery(String query) throws Exception {
        ResultSet set;
        Statement stmt = conn.createStatement();
        if(conn == null) throw new Exception();
        set = stmt.executeQuery(query);
        return set;
    }
    
    /**
     * Returns the existant instance of this class
     */
    public static SqlComm getInstance() {
        return myInstance;
    }
    
    /**
     * Returns current active connection to the DB
     */
    public Connection getConn() {
        return conn;
    }

    
    /**
     * Close the connection, destroy the class
     */
    public void destroy() {
        conn = null;
    }

}

/*

public class jdbc01 {

  public static void main (String args []) throws SQLException {

    System.out.println("\n-----  Start DEMO projektu jdbc01  -----\n");

    // Registrace ovladace
    System.out.println(" - probehla registrace ovladace na Oracle");
    
    // Ziskani spojeni
    System.out.println(" - vytvoreno spojeni s databazi (SRBD)");

    // Zisk metadat pro tisk verze
    DatabaseMetaData meta = conn.getMetaData ();
    System.out.println(" - verze JDBC je (z metadat) " + meta.getDriverVersion());

    // Vytvoreni prazdneho prikazu
    
    System.out.println(" - vytvoren prazdny prikaz");
    
    // Dotaz na DB
    ResultSet rss = stmt.executeQuery("select * from tab1");
    System.out.println(" - proveden dotaz 'select * from tab1'");

    // Pretypovani vysledku dotazu
    OracleResultSet rs = (OracleResultSet) rss;
    System.out.println(" - pretypovani vysledku na OracleResultSet\n");

    System.out.println("--- JMENO ---\t\t\t--- PLAT ---");
    while (rs.next()) {
      String jmeno = rs.getString("jmeno");
      int    plat  = rs.getInt("plat");

      jmeno = upravStr(jmeno,13);

      System.out.println(jmeno+"\t\t\t"+plat);
    }

    System.out.println("\n-----  Konec DEMO projektu  -----\n");

  }

  static String upravStr(String s,int d) {
     while (s.length() < d) s = s + " ";
     return s;
  }
}
*/
